<template>
	<div class="publicformview">
		<el-button type="primary" v-if='blockType==4||blockType==2||blockType==1' size="mini" @click="handleUpdate()">录入</el-button>
		<div class="bottomTop" style="margin-top: 10px;">
			<el-table max-height='500px' v-loading="listLoading" :data="list" border fit highlight-current-row size="mini">
				<el-table-column align="center" label="序号" prop="orderno">
				</el-table-column>
			
				<el-table-column align="center" label="日期" prop="date">
				</el-table-column>
				
				<el-table-column align="center" label="内容摘要" prop="contentmemo1">
				</el-table-column>
				
				<el-table-column align="center" label="操作" width="280">
					<template slot-scope="scope" v-if='blockType==4||blockType==2||blockType==1'>
						<el-button type="text" size="mini" @click="handleUpdate(scope.row.otherid)">修改</el-button>
					</template>
				</el-table-column>
			</el-table>
		</div>
		<div class="publicpaginationview">
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
				:current-page="listQuery.pageNum" background :page-sizes="[10, 20, 50, 100]" :page-size="listQuery.pageSize"
				layout="total, sizes, prev, pager, next, jumper" :total="total" small></el-pagination>
		</div>
		
		<el-dialog title="" :visible.sync="dialogVisible" width="50%" :before-close="handleClose">
			<el-form :model="addQuery" ref="dataForm" class="demo-ruleForm" label-position="right" label-width="150px">
				<el-form-item label="序号：" prop="orderno">
					<el-input v-model="addQuery.orderno"></el-input>
				</el-form-item>
				<el-form-item label="日期：" prop="date">
					<el-date-picker v-model="addQuery.date" type="date" placeholder="年/月/日">
					</el-date-picker>
				</el-form-item>
				<el-form-item label="内容摘要：" prop="contentmemo1">
					<el-input v-model="addQuery.contentmemo1"></el-input>
				</el-form-item>
			</el-form>
			<el-upload class="upload-demo" drag :action="uploadAction" :file-list="fileList" :on-success="handleUploadSuccess"
				:on-error="handleUploadError" :limit="1" :on-remove="handleFileRemove" :before-upload="beforeUpload" multiple>
				<i class="el-icon-upload"></i>
				<div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
			</el-upload>
			<div class="saveContent">
				<el-button type="primary" @click="save" size="medium">保 存</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import dayjs from 'dayjs'
	import {
		blockDetailOtherRepair,
		addBlockOther,
		blockOtherData,
		updBlockOther
	} from '@/api/industry'
	export default {
		props: {
			blockType: String,
			blockid: String,
			blockno: String
		},
		name: "fangwuxianzhuang",
		data() {
			return {
				listQuery: {
					pageNum: 1,
					pageSize: 20
				},
				addQuery: {
					orderno: '',
					date:'',
					contentmemo1:'',
					pic:''
				},
				total: 0,
				list: [],
				listLoading: false,
				dialogVisible: false,
				isChanquan: true,
				type: 1,
				mapfile: '',
				uploadAction: 'http://125.39.77.18:9021/mall-admin/fileUpload/upload',
				fileList: [],
        currentFileSrc: [],
			};
		},

		computed: {
		},

		mounted() {
			this.blockDetailOtherRepair()
		},

		methods: {
			blockDetailOtherRepair() {
				if(this.blockno?this.blockno:localStorage.getItem('blockno')){
					let data = {
						BlockNo: this.blockno?this.blockno:localStorage.getItem('blockno'),
						pageNum:this.pageNum,
						pageSize:this.pageSize
					}
					blockDetailOtherRepair(data).then(res => {
						console.log(res)
						this.list = res.data.list
						this.total = res.data.total
					})
				}
			},
			handleUpdate(otherid) {
				this.otherid = otherid
				this.dialogVisible = true;
				if(otherid){
					let data = {
						otherId:otherid
					}
					blockOtherData(data).then(res=>{
						console.log(res)
						this.addQuery = res.data
					})
				}
			},
			save() {
				if(this.blockno||localStorage.getItem('blockno')){
					let data = this.addQuery
					data.blockno = this.blockno?this.blockno:localStorage.getItem('blockno')
					data.date = dayjs(data.date).format('YYYY-MM-DD')
					if (this.otherid) {
						data.otherid = this.otherid
						updBlockOther(data).then(res => {
							console.log(res)
							this.dialogVisible = false;
							this.blockDetailOtherRepair()
							this.$message({
								type: 'success',
								message: '操作成功!'
							});
						})
					} else {
						addBlockOther(data).then(res => {
							console.log(res)
							this.dialogVisible = false;
							this.blockDetailOtherRepair()
							this.$message({
								type: 'success',
								message: '操作成功!'
							});
						})
					}
					this.reter()
				}else{
					this.$message({
						type: 'success',
						message: '请先提交产权情况!'
					});
				}
				
			},
			reter(){
				this.addQuery.orderno = '',
				this.addQuery.date = '',
				this.addQuery.contentmemo1 = ''
			},
			handleSizeChange(val) {
				this.listQuery.pageNum = 1;
				this.listQuery.pageSize = val;
				this.blockDetailOtherRepair()
			},
			handleCurrentChange(val) {
				this.listQuery.pageNum = val;
				this.blockDetailOtherRepair()
			},
			returns() {
				this.$router.go(-1)
			},
			handleClose(done) {
				this.$confirm('确认关闭？')
					.then(_ => {
						done();
						this.reter()
					})
					.catch(_ => {});
			},
			// 选择类型
			selectButton(type) {
				this.type = type
			},
			// 上传前验证
			beforeUpload(file) {
				const isExcel = file.type === 'application/vnd.ms-excel' ||
					file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
				// if (!isExcel) {
				//     this.$message.error('只能上传 Excel 文件！');
				//     return false;
				// }
			
				const isLt2M = file.size / 1024 / 1024 < 2;
				if (!isLt2M) {
					this.$message.error('上传文件大小不能超过 2MB!');
					return false;
				}
			
				return true;
			},
			// 文件选择变化
			handleFileRemove(file, fileList) {
				this.fileList = fileList;
				let arr = [...this.currentFileSrc];
				let i;
				arr.forEach((item, index) => {
					if (item.fileSize == file.size && item.name == file.name) {
						i = index
					}
				})
				arr.splice(i, 1);
				this.currentFileSrc = arr;
				console.log(this.currentFileSrc)
			
			},
			// 上传成功
			handleUploadSuccess(response, file, fileList) {
				this.fileList = fileList
				if (response.code == 200) {
					this.$message.success('文件上传成功');
					this.currentFileSrc.push(response.data)
				
					this.addQuery.pic = response.data.url
				} else {
					this.$message.error('文件上传失败');
					let arr = [...fileList.map(i => i)]
					arr.pop();
					this.fileList = arr;
			
				}
			},
			// 上传失败
			handleUploadError(err, file) {
				this.$message.error('文件上传失败');
			},
		},
	};
</script>

<style scoped>
	.headTop {
		width: 100%;
		display: flex;
		justify-content: space-between;

		.xinxi {
			display: flex;
			width: 800px;
		}
	}

	.el-form-item {
		justify-content: start !important;
	}

	.el-select {
		width: 180px;
	}

	.el-date-picker {
		width: 180px;
	}

	.bottomTop {
		width: 100%;
		display: flex;
		justify-content: end;
	}

	.tabButton {
		background: #EFF2F7;
		color: #333333;
		border: none;
	}

	.fangwuList {
		width:100%;
		background-color: #F5F5F5;
		margin: 3px auto 0;
		overflow: hidden;
	}

	.fangwuText {
		font-weight: bold;
		font-size: 16px;
		color: #008C93;
		margin-top: 20px;
		margin-left: 26px;
	}
	.saveContent {
		width: 100%;
		display: flex;
		justify-content: end;
		margin-top: 20px;
		padding-bottom: 20px;
	}
</style>